#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>

#define NUM 16
//const char *myfile = "/home/ljx/2023-linux/test10-13/mycmd";
const char *myfile = "./mycmd";


int main(int argc, char *argv[], char *env[])
{

    char * const _env[NUM] = {(char*)"MY_VAL=6666666666666", NULL};
    
    
    pid_t id = fork();
    if(id == 0)
    {   
        //子进程
        printf("子进程开始运行,pid:%d\n", getpid());
        sleep(3);
        char * const _argv[NUM] = {"ls", "-a", "-l", "-i", NULL};
        
        
        
        
        //execl("/usr/bin/ls", "ls", "-a", "-l", "-i", NULL); 
        //execlp("ls", "ls", "-a", "-l", "-i", NULL); 
        //execv("/usr/bin/ls", _argv);
        //execvp("ls", _argv);
        //execl(myfile, "mycmd", "-a", NULL);
        execle(myfile, "mycmd", "-a", NULL, _env);
        exit(1);

    }
    //父进程
    printf("父进程开始运行,pid:%d\n", getpid());
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);//阻塞等待,子进程先运行完毕，父进程获取后，才退出
    if(ret > 0)
    {
        printf("等待成功,退出码:%d\n", WEXITSTATUS(status));
        
    }
    
    return 0;




}
